package com.ofsize.keepmoving;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import com.facebook.drawee.view.SimpleDraweeView;
import com.ofsize.keepmoving.drag.ImgDragShadowBuilder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Dengyong on 2015/6/3.
 */

public class StaggeredImageAdapter extends RecyclerView.Adapter<StaggeredImageAdapter.StaggeredImageViewHolder> {

    private Context context;
    private List<String> imgUrls;
    private List<Integer> heights;
    private int dragPosition;
    private SimpleDraweeView dragImg;

    //public ImgDragListener imgDragListener = new ImgDragListener();

    class HolderTag {

        private int position;
        private String url;

        public HolderTag(int position, String url) {
            this.position = position;
            this.url = url;
        }
    }

    class StaggeredImageViewHolder extends RecyclerView.ViewHolder {

        SimpleDraweeView img;

        public StaggeredImageViewHolder(View view) {
            super(view);
            img = (SimpleDraweeView) view.findViewById(R.id.staggerd_img);
        }
    }

    public StaggeredImageAdapter(Context context, List<String> imgUrls) {
        this.context = context;
        this.imgUrls = imgUrls;

        heights = new ArrayList<Integer>();
        for (int i = 0; i < imgUrls.size(); i++)
            heights.add( (int) (100 + Math.random() * 200));
    }

    @Override
    public StaggeredImageViewHolder onCreateViewHolder(ViewGroup parent, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.staggered_img_item, parent, false);

        final StaggeredImageViewHolder holder = new StaggeredImageViewHolder(view);

        //holder.img.setOnDragListener(imgDragListener);
        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HolderTag tag = (HolderTag) (holder.img.getTag());
                showImg(tag.url);
            }
        });
        holder.img.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // 开始拖动
                dragPosition = holder.getPosition();
                dragImg = holder.img;
                v.startDrag(null, new ImgDragShadowBuilder(holder.img), null, 0);
                Animation shake = AnimationUtils.loadAnimation(context, R.anim.shake);
                v.startAnimation(shake);
                return true;
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(StaggeredImageViewHolder holder, int pos) {
        LayoutParams lp = holder.img.getLayoutParams();
        lp.height = heights.get(pos);

        String url = imgUrls.get(pos);
        holder.img.setLayoutParams(lp);
        holder.img.setImageURI(Uri.parse(url));
        holder.img.setTag(new HolderTag(holder.getPosition(), url));
    }

    @Override
    public int getItemCount() {
        return imgUrls.size();
    }

    /**
     * 拖动事件监听者
     */
    /*
    private class ImgDragListener implements View.OnDragListener {
        public boolean onDrag(View v, DragEvent event) {
            if (event.getAction() == DragEvent.ACTION_DROP) {
                //processDrop(v);
                v.invalidate();
            }
            return true;
        }
    }
    */

    /**
     * 处理拖动
     * @param v
     */
    /*
    private void processDrop(View v) {
        HolderTag vTag = (HolderTag)v.getTag();
        HolderTag dragImgTag = (HolderTag)dragImg.getTag();
        if (dragImgTag.position != vTag.position) {
            // 删除拖动的图片
            removeImg(dragPosition);
            // 插入到被拖动的图片之前
            insertImg(vTag.position, dragImgTag.url);
        }
    }
    */

    /**
     * 放大显示图片
     * @param url
     */
    private void showImg(String url) {
        Intent intent = new Intent();
        intent.setClass(context, ShowImgActivity.class);
        intent.putExtra("url", url);
        context.startActivity(intent);
        //((Activity)context).overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    /**
     * 删除图片
     * @param position
     */
    /*
    private void removeImg(int position) {
        imgUrls.remove(position);
        notifyItemRemoved(position);
    }
    */

    /**
     * 插入图片
     * @param position
     * @param url
     */
    /*
    private void insertImg(int position, String url) {
        imgUrls.add(position, url);
        this.notifyItemInserted(position);
    }
    */
}